package com.gateway.service.filter.pre;

import com.gateway.api.domain.contant.ReturnEnum;
import com.gateway.api.domain.vo.ApiInfo;
import com.gateway.api.util.BeanUtil;
import com.gateway.service.ApiService;
import com.gateway.service.annotation.GatewayFilterAttr;
import com.gateway.service.filter.GatewayFilter;
import com.gateway.service.filter.GatewayNotPassException;
import com.gateway.service.runner.RequestContext;

import javax.servlet.http.HttpServletRequest;

@GatewayFilterAttr()
public class CheckApiFilter extends GatewayFilter {
    @Override
    protected void run() {
        RequestContext ctx =RequestContext.getCurrentContext();
        HttpServletRequest servletRequest = ctx.getRequest();
        String URI = servletRequest.getRequestURI();
        ApiService apiService = BeanUtil.getBean(ApiService.class);
        if(!apiService.checkApiInfoIsExistsFromCache(URI)){
            throw  new GatewayNotPassException(ReturnEnum.NOT_FIND_API.getMsg()+"api:["+ctx.get("apiPath")+"]");
        }
    }
}
